package com.easyfun.healthmagicbox.comm;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import com.easyfun.healthmagicbox.sync.CommCallback;

import android.content.Context;
import android.util.Log;

public abstract class BaseRedoTask extends BaseCommTask {
	private final static String TAG = BaseRedoTask.class.getSimpleName();
	private Integer redoTaskID = 0;
	private Integer redoTaskTimes = 0;
	public final static String regExHead = "\\";
	public final static String TaskAndContentSeperator = "^~";
	public final static String ContentsSeperator = "|#";
	public BaseRedoTask(Context context,CommCallback callback)
	{
		super(context,callback);
	}

	//com.easyfun.healthmagicbox.comm.HealthInputDataUploadTask\^~9\^~1\^~185733326184295
	public String serialize() throws IllegalArgumentException
	{
		return getClass().getName() + BaseRedoTask.TaskAndContentSeperator +
				getRedoTaskID() + BaseRedoTask.TaskAndContentSeperator +
				getRedoTaskTimes() + BaseRedoTask.TaskAndContentSeperator +
				serializeParam();
	}
	
	
	public Integer getRedoTaskID() {
		return redoTaskID;
	}

	public void setRedoTaskID(Integer redoTaskID) {
		this.redoTaskID = redoTaskID;
	}

	public Integer getRedoTaskTimes() {
		return redoTaskTimes;
	}

	public void setRedoTaskTimes(Integer redoTaskTimes) {
		this.redoTaskTimes = redoTaskTimes;
	}

	public static BaseRedoTask deserialize(String serialstring,Context context,CommCallback callback)
	{
		String[] tmp =  serialstring.split(BaseRedoTask.regExHead + BaseRedoTask.TaskAndContentSeperator);
		if (tmp.length==4)
		{
			try {
				Constructor<?> c = Class.forName(tmp[0]).getConstructor(Context.class, CommCallback.class);
				BaseRedoTask task = (BaseRedoTask) c.newInstance(context,callback);
				//BaseRedoTask task = (BaseRedoTask) Class.forName(tmp[0]).newInstance();
				task.setRedoTaskID(Integer.valueOf(tmp[1]));
				task.setRedoTaskTimes(Integer.valueOf(tmp[2]));
				task.deserializeParam(tmp[3]);
				
				return task;
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		Log.e(TAG  , "Can't create a sync task!");
		return null;
	}

	public abstract String serializeParam() throws IllegalArgumentException;
	public abstract void  deserializeParam(String serialstring) throws IllegalArgumentException;
	
//	public void setCallBack(CommCallback ccb)
//	{
//		this.ccb = ccb;
//	}
//	
//	public void setContext(Context context)
//	{
//		mcontext = context;
//	}
	public Context getContext()
	{
		return mcontext;
	}	
}
